#include "jlib.h"
#include <string.h>

JPack* JLib::ReflectNextPack(char nameBuf[], DWORD bufSize)
{
	WORD mtype;
	while ((mtype = ReflectNextMember(nameBuf, bufSize)) != MT_NULL) {
		if (mtype == MT_JPACK) {
			JPack* jpack = GetPack(nameBuf);
			if (jpack->AccessAs(m_itemClassName)) {
				return(jpack);
			}
		}
	}
	return(NULL);
}

int JLib::GetList(char nameList[], DWORD listSize)
{
	WORD mtype;
	char mname[41];

	int n = 0;
	ReflectFirst();
	while ((mtype = ReflectNextMember(mname, 41)) != MT_NULL) {
		if (mtype == MT_JPACK) {
			JPack* jpack = GetPack(mname);
			if (jpack->AccessAs(m_itemClassName)) {
				n++;
				if (n == 1) {
					strcpy_s(nameList, listSize, mname);
				}
				else {
					strcat_s(nameList, listSize, ",");
					strcat_s(nameList, listSize, mname);
				}
			}
		}
	}
	return(n);
}

JPack* JLib::Query(const char* itemName)
{
	WORD mtype;
	char mname[41];

	ReflectFirst();
	while ((mtype = ReflectNextMember(mname, 41)) != MT_NULL) {
		if (mtype == MT_JPACK) {
			if (!strcmp(mname, itemName)) {
				JPack* jpack = GetPack(mname);
				if (jpack->AccessAs(m_itemClassName)) {
					return(jpack);
				}
			}
		}
	}
	return(NULL);
}
